<template>
  <div class="tab-bar-item" @click="itemClick">
    <!-- 使用div包裹插槽是因为防止某些属性被替换后失效 -->
    <div v-if="!isActive">
      <slot name="item-icon"></slot>
    </div>
    <div v-else>
      <slot name="item-icon-active"></slot>
    </div>
    <!-- <div :class="{active:isActive}"> -->
    <div :style="activeStyle">
      <slot name="item-text"></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: "TabBarItem",
  props: {
    path: String,/* 从父组件app传来的path */
    activeColor: {
      type: String,
      default: "rgb(244,143,177)"
    }
  },
  data() {
    return {
      // isActive: false
    };
  },
  computed: {
    /* 激活后的状态图片切换*/
    isActive() {
      return this.$route.path.indexOf(this.path) !== -1;
    },
    /* 动态绑定样式 */
    activeStyle() {
      return this.isActive ? { color: this.activeColor } : {};
    }
  },
  methods: {
    itemClick() {
      this.$router.replace(this.path);
    }
  }
};
</script>

<style scoped>
.tab-bar-item {
  /* 元素居中 */
  flex: 1;
  text-align: center;
  height: 49px;
  font-size: 14px;
  margin-top: 3px;
  vertical-align: middle;
}
.tab-bar-item img {
  width: 24px;
  height: 24px;
}
.active {
  color: rgb(0, 110, 255);
}
</style>